<template>
  <div style="margin-left: 20px;margin-right: 20px;margin-top: 20px;">
    <!--展示所有申请-->
    <div>
      <el-card class="box-card" style="margin-top: 20px">
        <el-table
            :data="tableData"
            style="width: 100%">
          <el-table-column
              prop="id"
              label="ID"
              v-if="false">
          </el-table-column>
          <el-table-column
              prop="username"
              label="申请学生姓名"
              width="180">
          </el-table-column>
          <el-table-column
              prop="title"
              label="申请课题名称"
              min-width="180">
          </el-table-column>
          <el-table-column
              prop="state"
              label="申请状态"
              width="180"
              :formatter="applyState">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.state !== null">
                <el-tag>已审批，无法进行操作</el-tag>
              </div>
              <div v-else>
                <el-button
                    size="mini"
                    type="primary"
                    @click="audit(scope.row, 0)">同意</el-button>
                <el-button
                    size="mini"
                    type="danger"
                    @click="audit(scope.row, 1)">拒绝</el-button>
              </div>

            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="block">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalNum">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { auditThesisApi, getAllApplyApi } from "@/api/ThesisApi";

export default {
  name: "Audit",
  data() {
    return {
      tableData:[],
      userId:this.$store.getters.GET_USERINFO.userId,
      totalNum: 0,
      currentPage: 1,
      pageSize:20
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAll(this.currentPage, this.pageSize)
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAll(this.currentPage, this.pageSize)
    },
    getAll(param1, param2) {
      let params = {
        current: param1,
        size: param2
      }
      getAllApplyApi(params).then(res => {
        if (res.code === 200) {
          this.totalNum = res.data.total
          this.tableData = res.data.records
        } else if (res.code === 401 || res.code === 403) {
          this.$message.error("获取失败")
        }
      })
    },
    audit(row, state) {
      let params= {
        thesisId: row.id,
        state: state
      }
      auditThesisApi(params).then(res => {
        if(res.code === 200) {
          this.$message.success("操作成功")
        } else if(res.code !== 401 || res.code !== 403){
          this.$message.error(res.msg)
        }
        this.getAll(this.currentPage, this.pageSize)
      })
    },
    applyState(row) {
      switch (row.state) {
        case 0:
          return <el-tag>已同意</el-tag>
        case 1:
          return <el-tag type="warning">已拒绝</el-tag>
        default:
          return <el-tag type="danger">待审核</el-tag>
      }
    },
  },
  created() {
    this.getAll(this.currentPage, this.pageSize)

  }
}
</script>

<style scoped>
.block {
  margin-top: 10px ;
  width: 100%;
  display:inline-block;
  text-align:center;
}
.search-btn {
  margin-left: 5px;
  display: inline-block;
  float: right;
}
</style>
